package com.litchi.dialogfragment;

import com.lib.uil.ToastUtils;
import com.litchi.R;
import com.litchi.app.SysApplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class ExitDialogFragment extends DialogFragment implements
		OnClickListener {

	private FragmentActivity mActivity;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActivity = getActivity();
		View inflate = inflater.inflate(R.layout.fragment_dialog_exit, null);
		inflate.findViewById(R.id.btn_ok).setOnClickListener(this);
		inflate.findViewById(R.id.btn_cancel).setOnClickListener(this);
		getDialog().getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.TRANSPARENT));
		getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		return inflate;
	}

	@Override
	public void onClick(View v) {
		dismiss();
		switch (v.getId()) {
		case R.id.btn_ok:

			if (SysApplication.getInstance().getMainActivity().getLogined()) {
				SysApplication.getInstance().getMainActivity()
						.setIsLogined(false, null);
				SharedPreferences sp = mActivity.getSharedPreferences(
						com.litchi.utils.Constants.USER_LOGIN_SP_KEY,
						Context.MODE_PRIVATE);
				sp.edit().clear().commit();
				mActivity.finish();
			} else {
				ToastUtils.showToast(mActivity, "还没登录呢...");
			}

			break;

		default:
			break;
		}
	}

}
